/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.thread
 * author:David
 * Date:2015年8月20日上午7:07:49
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.thread;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 线程范围内共享变量的概念与作用
 * @author David
 * @since 2015年8月20日 上午7:07:49
 * @version 
 * @since JDK 1.6
 */
public class ThreadScopeShareData {
	private static int data=0;
	private static Map<Thread,Integer> threadData=new HashMap<Thread,Integer>();
	public static void  main(String[] args) {
		for(int i=0;i<2;i++){
			new Thread(new Runnable() {
				@Override
				public void run() {
					int data=new Random().nextInt();
					System.out.println(Thread.currentThread().getName()
							+" has put data:"+data);
					threadData.put(Thread.currentThread(), data);
					new A().get();
					new B().get();
				}
			}).start();
		} 
	}

	static class A{
		public void get(){
			int data=threadData.get(Thread.currentThread());
			System.out.println("A from "+Thread.currentThread().getName()
					+" get data:"+data);
		
		}
	}
	
	static class B{
		public void get(){
			int data=threadData.get(Thread.currentThread());
			System.out.println("B from "+Thread.currentThread().getName()
					+" get data:"+data);
		
		}
	}
	
	
}

